home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / lpr / RCS / startdaemon.c,v < prev   
Encoding:
Text File  |  1990-02-16  |  3.1 KB  |  173 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     90.02.16.13.52.07;  author rab;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     88.11.23.10.31.55;  author rab;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @Changes for sparcStation.
  27. @
  28. text
  29. @/*
  30.  * Copyright (c) 1983 Regents of the University of California.
  31.  * All rights reserved.
  32.  *
  33.  * Redistribution and use in source and binary forms are permitted
  34.  * provided that this notice is preserved and that due credit is given
  35.  * to the University of California at Berkeley. The name of the University
  36.  * may not be used to endorse or promote products derived from this
  37.  * software without specific prior written permission. This software
  38.  * is provided ``as is'' without express or implied warranty.
  39.  */
  40.  
  41. #ifndef lint
  42. static char sccsid[] = "@@(#)startdaemon.c    5.2 (Berkeley) 5/5/88";
  43. #endif /* not lint */
  44.  
  45. /*
  46.  * Tell the printer daemon that there are new files in the spool directory.
  47.  */
  48.  
  49. #include <stdio.h>
  50. #include <sys/types.h>
  51. #include <sys/socket.h>
  52. #include <sys/file.h>
  53. #include "lp.local.h"
  54.  
  55. static perr();
  56.  
  57. startdaemon(printer)
  58.     char *printer;
  59. {
  60. #ifndef sprite
  61.         struct sockaddr_un sun;
  62. #endif
  63.     register int s, n;
  64.     char buf[BUFSIZ];
  65.  
  66. #ifdef sprite
  67.     gethostname(buf, sizeof(buf));
  68.     sprintf(pdevName, "/hosts/%s/dev/printer", buf);
  69.     s = open (pdevName, O_WRONLY, 0);
  70.     if (s < 0) {
  71.         perror(pdevName);
  72.         /*
  73.          * the daemon may be dead, so we attempt to start up a new one
  74.          */
  75.          switch (fork()) {
  76.  
  77.          case 0:
  78.             fprintf(stderr, "attempting to restart lpd\n");
  79.             execl("/sprite/daemons.$MACHINE/lpd", "lpd", 0);
  80.         perror("exec failed");
  81.         return 0;
  82.  
  83.          case -1:
  84.             perror("cannot fork");
  85.         return 0;
  86.  
  87.          default:
  88.             /* loop while we wait for the daemon to start up */
  89.             for (n = 60; --n >= 0;) {
  90.             sleep(1);
  91.             if ((s = open (pdevName, O_WRONLY, 0)) >= 0)
  92.             break;
  93.         }
  94.         if (s < 0)
  95.             return 0;
  96.         fprintf(stderr, "lpd restarted\n");
  97.         break;
  98.          }
  99.     }
  100.  
  101.     (void) sprintf(buf, "\1%s\n", printer);
  102.     n = strlen(buf);
  103.     if (write(s, buf, n) != n) {
  104.         perr("write");
  105.         (void) close(s);
  106.         return(0);
  107.     }
  108.     (void) close(s);
  109.     return(1);
  110. #else
  111.     s = socket(AF_UNIX, SOCK_STREAM, 0);
  112.     if (s < 0) {
  113.         perr("socket");
  114.         return(0);
  115.     }
  116.     sun.sun_family = AF_UNIX;
  117.     strcpy(sun.sun_path, SOCKETNAME);
  118.     if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
  119.         perr("connect");
  120.         (void) close(s);
  121.         return(0);
  122.     }
  123.     (void) sprintf(buf, "\1%s\n", printer);
  124.     n = strlen(buf);
  125.     if (write(s, buf, n) != n) {
  126.         perr("write");
  127.         (void) close(s);
  128.         return(0);
  129.     }
  130.     if (read(s, buf, 1) == 1) {
  131.         if (buf[0] == '\0') {        /* everything is OK */
  132.             (void) close(s);
  133.             return(1);
  134.         }
  135.         putchar(buf[0]);
  136.     }
  137.     while ((n = read(s, buf, sizeof(buf))) > 0)
  138.         fwrite(buf, 1, n, stdout);
  139.     (void) close(s);
  140.     return(0);
  141. #endif
  142. }
  143.  
  144. static
  145. perr(msg)
  146.     char *msg;
  147. {
  148.     extern char *name;
  149.     extern int sys_nerr;
  150.     extern char *sys_errlist[];
  151.     extern int errno;
  152.  
  153.     printf("%s: %s: ", name, msg);
  154.     fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout);
  155.     putchar('\n');
  156. }
  157.  
  158. @
  159.  
  160.  
  161. 1.1
  162. log
  163. @Initial revision
  164. @
  165. text
  166. @a23 3
  167. #ifndef sprite
  168. #include <sys/un.h>
  169. #else
  170. a24 1
  171. #endif
  172. @
  173.